const VisitLog = require('../models/index').getModel('visitLog')

const visitLogService = {
    async visitCountPerDay(options) {
        let time = 0
        if (options.time) {
            time = +options.time
        }
        let zone = -new Date().getTimezoneOffset() / 60
        if (zone > 0) {
            zone = '+0' + zone
        } else {
            zone = '-0' + zone
        }
        return VisitLog.aggregate(
            [
                {$match: {ts: {$gte: time}}},
                {
                    $project: {
                        dt: {
                            $dateToString: {
                                format: "%Y-%m-%d",
                                date: {$add: [new Date(0), "$ts"]},
                                timezone: zone
                            }
                        }
                    }
                },
                {$group: {_id: "$dt", count: {$sum: 1}}},
                {$sort: {_id: -1}}
            ])
    },
    async insertOneLog(log) {
        return VisitLog.create(log)
    },
}

module.exports = visitLogService
